/******************************************************************************
 * Copyright (C) 2025 EternalChip, Inc.(Gmbh) or its affiliates.
 * 
 * All Rights Reserved.alignas
 * @file ec_bsp_aht21_driver.h
 * 
 * @par dependencies
 * - ec_bsp_aht21_reg
 * - stdio.h
 * - stdint.h
 * 
 * @auther aether 
 * 
 * @brief Provide the HAL APIs of AHT21 and corresponding opetions
 * 
 * Processing flow:
 * 
 * call directly. 
 * 
 * @version V1.0 2025-07-08
 * 
 * @note 1 tab == 4 spaces!
 * 
 *****************************************************************************/
#ifndef __EC_BSP_AHT21_DRIVER_H__
#define __EC_BSP_AHT21_DRIVER_H__

//******************************* Includes **********************************//
#include "ec_bsp_aht21_reg.h"

#include <stdio.h>
#include <stdint.h>

//******************************* Defines ***********************************//

#define OS_SUPPORTING


/* Return values from function */
typedef enum 
{
    AHT21_OK                = 0,    /* Operation completed successfully     */
    AHT21_ERROR             = 1,    /* Run-time error without case matched  */
    AHT21_ERRORTIMEOUT      = 2,    /* Operation failed with timeout        */
    AHT21_ERRORSOURCE       = 3,    /* Resource not available               */
    AHT21_ERRORPARAMETER    = 4,    /* Parameter error                      */
    AHT21_ERRORNOMEMORY     = 5,    /* Out of memory                        */
    AHT21_ERRORISR          = 6,    /* Not alllowed in ISR context          */
    AHT21_RESERVED          = 0xFF  /* Reserved                             */
}aht21_status_t;

/* From Core Layer : IIC Port */
#ifndef HARDWARE_IIC /* True : Software IIC */
typedef struct 
{
    aht21_status_t (*pf_iic_init) (void*);          /* IIC init interface   */
    aht21_status_t (*pf_iic_deinit) (void*);        /* IIC deinit interface */
    aht21_status_t (*pf_iic_start) (void*);         /* IIC start interface  */
    aht21_status_t (*pf_iic_stop) (void*);          /* IIC stop interface   */
    aht21_status_t (*pf_iic_wait_ack) (void*);      /* IIC w-ack interface  */
    aht21_status_t (*pf_iic_send_ack) (void*);      /* IIC s-ack interface  */
    aht21_status_t (*pf_iic_send_no_ack) (void*);   /* IIC s-n-ack interface*/
    aht21_status_t (*pf_iic_send_byte) (void*,      /* IIC s-byte interface */
                                        const uint8_t);
    aht21_status_t (*pf_iic_receive_byte) (void*,   /* IIC r-byte interface */
                                           uint8_t * const);
    aht21_status_t (*pf_critical_enter) (void);     /* enter critical states*/
    aht21_status_t (*pf_critical_exit) (void);      /* exit critical state  */
}iic_driver_interface_t;
#endif /* end of HARDWARE_IIC */


#ifdef HARDWARE_IIC /* True : Hardware IIC */
typedef struct 
{
    aht21_status_t (*pf_iic_init) (void*);          /* IIC init interface   */
    aht21_status_t (*pf_iic_deinit) (void*);        /* IIC deinit interface */
    aht21_status_t (*pf_iic_send_ack) (void*);      /* IIC s-ack interface  */
    aht21_status_t (*pf_iic_send_no_ack) (void*);   /* IIC s-n-ack interface*/
    aht21_status_t (*pf_iic_send_byte) (void*);     /* IIC s-byte interface */
    aht21_status_t (*pf_iic_receive_byte) (void*);  /* IIC r-byte interface */
}iic_driver_interface_t;
#endif /* end of HARDWARE_IIC */

/* From Core Layer : TimeBase*/
typedef struct 
{
    uint32_t (*pfget_tick_count)(void);     /* Get Tick number interf   */
}timebase_interface_t;

#ifdef OS_SUPPORTING
typedef struct
{
    void (*pf_rtos_yield)(const uint32_t);  /* OS not-blocking delay    */
}yield_interface_t;
#endif /* end of OS_SUPPORTING */

/* Aht21_hal_driver instance calss  */
typedef struct
{
    iic_driver_interface_t  *p_iic_driver_instance;
    timebase_interface_t    *p_timebase_instance;
    yield_interface_t       *p_yield_instance;

    uint8_t (*pfinst)(void * const paht21_instance,
                      iic_driver_interface_t * const piic_driver_instance,
                      timebase_interface_t * const ptimebase_instance,
                      yield_interface_t * const yield_instance);
    
    uint8_t (*pfinit)(void * const);                /* AHT21 init function  */
    uint8_t (*pfdeinit)(void * const);              /* AHT21 deinit function*/
    uint8_t (*pfread_id)(void * const);             /* AHT21 read id        */
    uint8_t (*pfread_temp_humi)(void * const,       /* AHT21 read temp.humi */
                                float * const temp,
                                float * const humi);
    uint8_t (*pfread_humidity)(void * const,        /* AHT21 read humidity  */
                              float * const humi);
    uint8_t (*pfsleep)(void * const);               /* AHT21 sleep          */
    uint8_t (*pfwakeup)(void * const);              /* AHT21 Wake_up        */
}bsp_aht21_driver_t;

/* AHT21_hal_driver instance class Inst */
uint8_t aht21_inst(bsp_aht21_driver_t * const paht21_instance,
                   iic_driver_interface_t * const piic_driver_instance,
                   timebase_interface_t * const ptimebase_instance,
                   yield_interface_t * const yield_instace);

#endif /* __EC_BSP_AHT21_DRIVER_H__ */